
#include "setting_restore.h"
#include "pt_ipc.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_setting_restore_t  ui_set_restore;

static  char * icon_restore[1] = 
{
    "Warning.png",
};

static int setting_restore_cancel_clicked_event(void)
{
    debug_d("restore cancel\n");

    ui_set_restore.timeout = 6;

    if(ui_set_restore.reset_time){
        lv_timer_pause(ui_set_restore.reset_time);
    }    

    page_pop();
    root_gesture_enable();

    return 0;
}

static void setting_restore_cancel_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {
        setting_restore_cancel_clicked_event();
    }
}

static int setting_restore_yes_clicked_event(void)
{
    if(ui_set_restore.timeout > 0)
        return -1;
    //send recfg net cmd
    uint8_t flag = 1;
    pt_recnnt_flag_set(flag);
    pt_ipc_send_msg(IPC_MSG_RECNNT_STAT,(char*)(&flag),1);
    debug_d("re cfg_net \n");
    //
    //debug_d("go cfg_net page\n");
    //page_install("cfg_net",lv_page_config_network_create());
    //page_push_anim("cfg_net",LOAD_ANIM_MOVE_LEFT);


    return 0;
}

static void setting_restore_yes_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {
        setting_restore_yes_clicked_event();
    }
}

static int setting_restore_status_update(void)
{
    char txt_buff[256];

    if(ui_set_restore.timeout <= 0)
        return 0;

    ui_set_restore.timeout--;

    memset(txt_buff,0,sizeof(txt_buff));

    if(ui_set_restore.timeout == 0)
    {
        snprintf(txt_buff,256,"重新配网");
        lv_obj_set_style_bg_opa(ui_set_restore.bg_yes,LV_OPA_100,LV_PART_MAIN);    
        lv_obj_set_style_text_opa(ui_set_restore.lab_yes,LV_OPA_100,LV_PART_MAIN);
    }
    else
    {
        snprintf(txt_buff,256,"重新配网(%ds)",ui_set_restore.timeout);
    }

    lv_label_set_text(ui_set_restore.lab_yes,txt_buff);

    return 0;
}

void setting_restore_timer_callback(struct _lv_timer_t * t)
{
    setting_restore_status_update();
}

int ui_setting_restore_create(lv_obj_t *superview)
{
    char txt_buff[256];

    ui_set_restore.timeout = 6;

    ui_set_restore.img_tip = lv_img_create(superview);
    lv_img_set_src(ui_set_restore.img_tip,pt_fs_get(icon_restore[0]));
    lv_obj_set_pos(ui_set_restore.img_tip,200,22);
    //**********************************************************************************************
    ui_set_restore.lab_tip = lv_label_create(superview);
    lv_obj_set_pos(ui_set_restore.lab_tip,24,124);
    lv_obj_set_size(ui_set_restore.lab_tip,432,72);
    lv_obj_set_style_text_align(ui_set_restore.lab_tip,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_restore.lab_tip,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_restore.lab_tip,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_restore.lab_tip, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_restore.lab_tip, "重新配网将清除账号、系统设置等所有本地数据，开关会恢复为关闭状态。");
    //**********************************************************************************************
    //cancel btn

    ui_set_restore.bg_cancel = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_restore.bg_cancel,60,246);
    lv_obj_set_size(ui_set_restore.bg_cancel,170,50);

	lv_obj_set_style_radius(ui_set_restore.bg_cancel, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_restore.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_restore.bg_cancel, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_restore.bg_cancel, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_restore.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_restore.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_restore.bg_cancel, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_restore.bg_cancel, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_restore.bg_cancel,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_restore.bg_cancel,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_restore.bg_cancel,lv_color_make(0x26,0x7a,0xff),0);
    #endif

    lv_obj_add_event_cb(ui_set_restore.bg_cancel, setting_restore_cancel_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_restore.lab_cancel = lv_label_create(ui_set_restore.bg_cancel);
    lv_obj_set_pos(ui_set_restore.lab_cancel,0,5);
    lv_obj_set_size(ui_set_restore.lab_cancel,170,30);
    lv_obj_set_style_text_align(ui_set_restore.lab_cancel,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_restore.lab_cancel,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_restore.lab_cancel,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_restore.lab_cancel, lv_font_get(28), LV_STATE_DEFAULT);

    lv_label_set_text(ui_set_restore.lab_cancel,"取消");
    //**********************************************************************************************
    //**********************************************************************************************
    //yes btn

    ui_set_restore.bg_yes = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_restore.bg_yes,275,246);
    lv_obj_set_size(ui_set_restore.bg_yes,170,50);

	lv_obj_set_style_radius(ui_set_restore.bg_yes, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_restore.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_restore.bg_yes, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_restore.bg_yes, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_restore.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_restore.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_restore.bg_yes, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_restore.bg_yes, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_restore.bg_yes,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_restore.bg_yes,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_restore.bg_yes,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_set_restore.bg_yes, setting_restore_yes_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_restore.lab_yes = lv_label_create(ui_set_restore.bg_yes);
    lv_obj_set_pos(ui_set_restore.lab_yes,0,5);
    lv_obj_set_size(ui_set_restore.lab_yes,170,30);
    lv_obj_set_style_text_align(ui_set_restore.lab_yes,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_restore.lab_yes,LV_OPA_30,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_restore.lab_yes,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_restore.lab_yes, lv_font_get(28), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"重新配网(%ds)",ui_set_restore.timeout);

    lv_label_set_text(ui_set_restore.lab_yes,txt_buff);
    //**********************************************************************************************
    if(!ui_set_restore.reset_time){
        ui_set_restore.reset_time = lv_timer_create(setting_restore_timer_callback,1000,NULL);
    }else {
        lv_timer_reset(ui_set_restore.reset_time);
    }
    //**********************************************************************************************

    return 0;
}

int ui_setting_restore_destroy(lv_obj_t *superview)
{
    ui_set_restore.timeout = 6;
    if(ui_set_restore.img_tip){
        lv_obj_del(ui_set_restore.img_tip);
        ui_set_restore.img_tip = NULL;
    }
    if(ui_set_restore.lab_tip){
        lv_obj_del(ui_set_restore.lab_tip);
        ui_set_restore.lab_tip = NULL;
    }
    if(ui_set_restore.bg_cancel){
        lv_obj_del(ui_set_restore.bg_cancel);
        ui_set_restore.bg_cancel = NULL;
        ui_set_restore.lab_cancel = NULL;
    }
    if(ui_set_restore.bg_yes){
        lv_obj_del(ui_set_restore.bg_yes);
        ui_set_restore.bg_yes = NULL;
        ui_set_restore.lab_yes = NULL;
    }
    if(ui_set_restore.reset_time){
        lv_timer_del(ui_set_restore.reset_time);
        ui_set_restore.reset_time = NULL;
    }

    return 0;
}

static void on_setting_restore_custom_attr_config(page_base_t* self)
{
    debug_d("custom_attr_config\n");
    
}

static void on_setting_restore_view_load(page_base_t* self)
{
    debug_d("view_load\n");
    
}

static void on_setting_restore_view_did_load(page_base_t* self)
{
    debug_d("create\n");
    ui_setting_restore_create(self->root);
}

static void on_setting_restore_view_will_appear(page_base_t* self)
{
    debug_d("view_will_appear\n");

}
static void on_setting_restore_view_did_appear(page_base_t* self)
{
    debug_d("view_did_appear\n");

}
static void on_setting_restore_view_will_disappear(page_base_t* self)
{
    debug_d("view_will_disappear\n");

}
static void on_setting_restore_view_did_disappear(page_base_t* self)
{
    debug_d("view_did_disappear\n");

}
static void on_setting_restore_view_did_unload(page_base_t* self)
{
    debug_d("view_did_unload\n");
    ui_setting_restore_destroy(self->root);

}
static page_vtable_t setting_restore_param = {
    .on_custom_attr_config  = on_setting_restore_custom_attr_config,
    .on_view_load           = on_setting_restore_view_load,
    .on_view_did_load       = on_setting_restore_view_did_load,
    .on_view_will_appear    = on_setting_restore_view_will_appear,
    .on_view_did_appear     = on_setting_restore_view_did_appear,
    .on_view_will_disappear = on_setting_restore_view_will_disappear,
    .on_view_did_disappear  = on_setting_restore_view_did_disappear,
    .on_view_did_unload     = on_setting_restore_view_did_unload,
};


page_vtable_t* lv_page_setting_restore_create(void)
{
    return &setting_restore_param;
}
